<?php

/**
 * 话机会员卡
 *
 * Class TworldController
 */
class TworldController extends BaseController
{
    use \app\library\traits\Restful;

    public function init()
    {
        parent::init();
        $this->checkLogin();
    }


    public function getCardAction()
    {
        $keyval = $this->getRequest()->getQuery('keyval');
        if (empty($keyval)) {
           return $this->noParams();
        }
        $card = (new TworldCardModel())->findByKeyval($keyval);
        if (empty($card)) {
            return  $this->returnError(10031, '不存在该类型的会员卡');
        }

        $UserTworldCardModel = new UserTworldCardModel();
        $list = $UserTworldCardModel->getByUserAndCardid($this->user_id, $card['id']);
        $data = [
            'dredge' => 1,
            'info' => [],
            'vouchers' => [],
            'card' => $card
        ];
        if (!empty($list) && $list['status'] != $UserTworldCardModel::STATUS['back']) {

            $TworldBusiness = new \app\business\TworldBusiness();
            if ($list['status'] == 1) {
                //检测是否过期
                if ($TworldBusiness->checkExpirationTime($list['expiration_time'])) {
                    $UserTworldCardModel->updateStatusToPast($list['id']);
                    $list['status'] = $UserTworldCardModel::STATUS['overdue'];
                }

                //获取优惠券列表
                $UserTworldVoucherModel = new UserTworldVoucherModel();
                $vouchers = $UserTworldVoucherModel->getListByUser($this->user_id, $list['id']);
                if ($vouchers) {
                    foreach ($vouchers as &$voucher) {
                        //检测 优惠券是否过期
                        if ($voucher['status'] == $UserTworldVoucherModel::STATUS['normal']
                            && $TworldBusiness->checkExpirationTime($voucher['expiration_time'])) {
                            $voucher['status'] = $UserTworldVoucherModel::STATUS['overdue'];
                            //更新数据
                            $UserTworldVoucherModel->updateById([
                                'status' => $UserTworldVoucherModel::STATUS['overdue']
                            ], $voucher['id']);
                        }

                        //特殊处理贴膜券
                        if ($voucher['category'] == 2) {
                            $cratetime = strtotime($voucher['createtime']);
                            if ($voucher['vaild_type'] == 2) {
                                $voucher['start'] = date('Y.m.d', $cratetime);
                                $voucher['end'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                            } else if ($voucher['vaild_type'] == 3) {
                                $voucher['start'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                                $voucher['end'] = date('Y.m.d', strtotime($voucher['expiration_time']));
                            }
                        }
                    }
                }

                $data['vouchers'] = $vouchers;
            }
            $data['info'] = $list;
        } else {
            $data['dredge'] = 0;

            //根据会员卡 获取该会员卡的优惠券信息
            $TworldVoucherModel = new TworldVoucherModel();
            $vouchers = $TworldVoucherModel->getListByCardid($card['id']);

            if ($vouchers) {
                foreach ($vouchers as &$voucher) {
                    //特殊处理贴膜券
                    if ($voucher['category'] == 2) {
                        $cratetime = time();
                        if ($voucher['vaild_type'] == 2) {
                            $voucher['start'] = date('Y.m.d', $cratetime);
                            $voucher['end'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                        } else if ($voucher['vaild_type'] == 3) {
                            $voucher['start'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                            $voucher['end'] = date('Y.m.d', strtotime('+2 year', $cratetime));
                        }
                    }
                }
            }

            $data['vouchers'] = $vouchers;
        }
        return $this->returnSuccess($data);
    }

    /**
     * 检测会员状态
     */
    public function checkStatusAction()
    {
        $UserTworldCardModel = new UserTworldCardModel();
        $list = $UserTworldCardModel->getByUser($this->user_id);
        $data = [
            'dredge' => 1,
        ];
        if (!empty($list)) {
            $TworldBusiness = new \app\business\TworldBusiness();
            if ($list['status'] == $UserTworldCardModel::STATUS['normal']) {
                //检测是否过期
                if ($TworldBusiness->checkExpirationTime($list['expiration_time'])) {
                    $UserTworldCardModel->updateStatusToPast($list['id']);
                    $list['status'] = $UserTworldCardModel::STATUS['overdue'];
                    $data['dredge'] = 0;
                }
            }

            //如果过期
            if ($list['status'] == $UserTworldCardModel::STATUS['overdue']) {
                //计算出具体的过期天数
                $end = strtotime($list['expiration_time']);
                $now = strtotime(date('Y-m-d'));

                $data['expiration_day'] = (int) ($now - $end) / 86400;
            }
            $data['expiration_time'] = $list['expiration_time'];
            $data['status'] = $list['status'];
        } else {
            $data['dredge'] = 0;
        }

        return $this->returnSuccess($data);
    }

    /**
     * 获取所有的卡
     * 之后将取代 checkStatusAction()
     */
    public function cardListAction(){
        $TworldCardModel = new TworldCardModel();
        $cards = $TworldCardModel->getAll();

        if ($cards) {
            //获取我的会员卡
            $UserTworldCardModel = new UserTworldCardModel();
            $user_cards = $UserTworldCardModel->getAllByUser($this->user_id);
            if ($user_cards) {
                $user_cards = array_column($user_cards, null, 'tworld_card_id');
            }
            $TworldBusiness = new \app\business\TworldBusiness();
            foreach ($cards as &$card) {
                if (isset($user_cards[$card['id']])) {
                    $card['status'] = $user_cards[$card['id']]['status'];
                    $card['expiration_time'] = $user_cards[$card['id']]['expiration_time'];

                    //根据时间修改状态
                    if ($user_cards[$card['id']]['status'] == $UserTworldCardModel::STATUS['normal']) {
                        //过期
                        if ($TworldBusiness->checkExpirationTime($user_cards[$card['id']]['expiration_time'])) {
                            $card['status'] = $UserTworldCardModel::STATUS['overdue'];
                        }
                    }

                    //如果过期
                    if ($card['status'] == $UserTworldCardModel::STATUS['overdue']) {
                        //计算出具体的过期天数
                        $end = strtotime($card['expiration_time']);
                        $now = strtotime(date('Y-m-d'));

                        $card['expiration_day'] = (int) ($now - $end) / 86400;
                    }

                } else {
                    $card['status'] = '-1';
                }
            }
            unset($card);
        }
        return $this->returnSuccess($cards);
    }

    /**
     * 获取详情
     */
    public function voucherInfoAction()
    {
        $id = $this->getRequest()->getQuery('id');
        if (empty($id)) {
            $this->noParams();
        }

        $list = (new UserTworldVoucherModel())->getVoucherInfo($id);
        if ($list) {
            $cratetime = strtotime($list['createtime']);
            $list['start'] = date('Y.m.d', $cratetime);
            $list['end'] = date('Y.m.d', strtotime($list['expiration_time']));
            if ($list['category'] == 2) {
                if ($list['vaild_type'] == 2) {
                    $list['end'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                } else if ($list['vaild_type'] == 3) {
                    $list['start'] = date('Y.m.d', strtotime('+1 year', $cratetime));
                }

            }

        }

        return $this->returnSuccess($list);
    }


    public function getStoreAction(){
        $keyval = $this->getRequest()->getQuery('keyval');
        if (empty($keyval) || !in_array($keyval,['PF','PC', 'YD', 'XM'])) {
            return $this->noParams();
        }

        $page  = $this->getRequest()->getQuery('page', 1);
        $loadend = false;

        if ($keyval == 'YD') {
            $bind = [
                'PF' => '苹果授权体验店',
                'PC' => '华为授权体验店',
                'YD' => '中国移动营业厅',
                'XM' => '小米授权体验店'
            ];
            $list = (new PhysicalStoreModel())->findListByCat($bind[$keyval], $page, 20);

            if (empty($list) || count($list) < 20) {
                $loadend = true;
            }

        } else {
            $list = match ($keyval) {
                'XM' => $this->getXM(),
                'PF' => $this->getPF(),
                'PC' => $this->getPC(),
            };
            $loadend = true;
        }

        return $this->returnSuccess([
            'list' => $list,
            'loadend' => $loadend ? 1 : 0
        ]);
    }

    private function getXM() :array{
        $XM = '[
        {
            "title":"小米之家（滨江宝龙城店）",
            "address":"浙江省杭州市滨江区滨盛路3867号宝龙一层小米专卖店"
        },
        {
            "title":"小米之家（杭州乐堤港店）",
            "address":"浙江省杭州市拱墅区远洋乐堤港负一层小米店"    
        },
        {
            "title":"小米之家（东吴银泰店）",
            "address":"浙江省湖州市东吴银泰城Ａ馆05-06铺小米之家"
        },
        {
            "title":"小米之家（海宁银泰店）",
            "address":"浙江省海宁市海昌南路365号银泰城购物中心1F103-A 小米专卖店"
        },
        {
            "title":"小米之家（慈溪吾悦店）",
            "address":"浙江省慈溪市浒山街道新城大道南路1449号慈溪新城吾悦广场1049"
        },
        {
            "title":"小米之家（天一广场水晶街店）",
            "address":"浙江省宁波市海曙区天一广场水晶街33号"
        },
        {
            "title":"小米之家（玉环吾悦店）",
            "address":"浙江省玉环市双港路8号玉环新城吾悦广场1031铺位（小米专卖店）"
        },
        {
            "title":"小米之家（瑞安吾悦店）",
            "address":"浙江省瑞安市瑞祥大道1158号新城吾悦1F1025号.小米店"
        },
        {
            "title":"小米之家（湖州红旗路卫星店）",
            "address":"浙江省湖州市吴兴区红旗路1号湖州大厦1楼"
        },
        {
            "title":"小米之家（杭州浦沿卫星店）",
            "address":"浙江省杭州市滨江区浦沿街道浦沿路729-731号移动营业厅"
        },
        {
            "title":"小米之家（玉环坎门海城卫星店）",
            "address":"浙江省台州市玉环县坎门海城路1号"
        },
        {
            "title":"小米之家（移动延安路卫星店）",
            "address":"浙江省杭州市下城区延安路391号移动营业厅"
        },
        {
            "title":"小米之家（下沙话机世界综合卫星店）",
            "address":"浙江省杭州市下沙4号大街9-22小米店"
        },
        {
            "title":"小米之家（萧山市心广场卫星店）",
            "address":"浙江省杭州市萧山区市心广场A座1010号话机世界"
        },
        {
            "title":"小米之家（星光大道卫星店）",
            "address":"浙江省杭州市滨江区江南大道288号康恩贝大厦1楼移动营业厅"
        },
        {
            "title":"小米之家（体育场路卫星店）",
            "address":"浙江省杭州市下城区体育场路店371号话机世界"
        },
        {
            "title":"小米之家（瑞安莘塍卫星店）",
            "address":"浙江省温州瑞安市莘塍镇镇府路商业大厦"
        }
        ]';

        return json_decode($XM, true);
    }
    private function getPF() :array{
        $PF =  '[{
                "title": "T-WORLD品丰杭州西溪印象城店",
				"address": "浙江省杭州市余杭区五常大道1号西溪印象城1楼L137B"
			},
			{
                "title": "T-WORLD品丰绍兴柯桥万达店",
				"address": "浙江省绍兴市柯桥区钱陶路799号万达广场1楼1-509"
			},
			{
                "title": "T-WORLD品丰宁波北仑银泰城店",
				"address": "浙江省宁波市北仑区中河路和岷山路交叉口北仑银泰城1楼1077号"
			},
			{
                "title": "T-WORLD品丰余姚五彩城店",
				"address": "浙江省余姚市城东路金型路口五彩城1楼122A"
			},
			{
                "title": "T-WORLD品丰绍兴金帝银泰城店",
				"address": "浙江省绍兴市越城区解放南路777号金帝银泰城1楼1F54"
			},
			{
                "title": "T-WORLD品丰杭州中大银泰城店",
				"address": "浙江省杭州市下城区东新路822号中大银泰城1楼L121"
			},
			{
                "title": "T-WORLD品丰杭州拱墅万达店",
				"address": "浙江省杭州市拱墅区石祥路和杭行路交叉口拱墅万达广场1楼1002"
			},
			{
                "title": "T-WORLD品丰诸暨雄风新天地店",
				"address": "浙江省诸暨市苎萝东路555号雄风新天地一楼西门苹果店"
			},
			{
                "title": "T-WORLD品丰海宁银泰店",
				"address": "浙江省海宁市海昌南路365号海宁银泰城1楼126-A"
			},
			{
                "title": "T-WORLD品丰温岭银泰城店",
				"address": "浙江省温岭市中华北路728号银泰城1楼"
			},
			{
                "title": "T-WORLD品丰温州万象城店",
				"address": "浙江省温州市瓯海区温瑞大道999号万象城1楼L129"
			},
			{
                "title": "T-WORLD品丰嘉兴龙鼎万达店",
				"address": "浙江省嘉兴市南湖区龙凤路与新盛西路交叉口经开万达广场一楼苹果店"
			},
			{
                "title": "T-WORLD品丰瑞安吾悦广场店",
				"address": "浙江省温州市瑞安市瑞祥大道与逻阳大道交叉口吾悦广场一楼L1011"
			},
			{
                "title": "T-WORLD品丰义乌吾悦广场店",
				"address": "浙江省义乌市宾王路与江东东路交叉口吾悦广场一楼苹果店"
			},
			{
                "title": "T-WORLD品丰杭州大悦城店",
				"address": "浙江省杭州市拱墅区隐秀路1号大悦城一楼苹果店"
			},
			{
                "title": "T-WORLD品丰慈溪吾悦广场店",
				"address": "浙江省宁波市慈溪市南三环路与新城大道交叉口吾悦广场一楼苹果店"
			},
			{
                "title": "T-WORLD品丰杭州下沙印象城店",
				"address": "浙江省杭州市江干区金沙大道97号金沙印象城一楼苹果店"
			},
			{
                "title": "T-WORLD品丰杭州西溪银泰城店",
				"address": "浙江省杭州市西湖区双龙街588西溪银泰城一楼009"
			},
			{
                "title": "T-WORLD品丰宁波环球银泰城店",
				"address": "浙江省宁波市鄞州区天童南路1088号环球银泰城一楼T-WORLD"
			},
			{
                "title": "T-WORLD品丰绍兴上虞万和城店",
				"address": "浙江省绍兴市上虞区市民大道688号上虞万和城一楼T-WORLD"
			},
			{
                "title": "T-WORLD品丰杭州临平银泰城店",
				"address": "浙江省杭州市余杭区世纪大道西1号临平银泰城一楼133 T-WORLD"
			},
			{
                "title": "T-WORLD品丰杭州之江银泰百货店",
				"address": "浙江省杭州市西湖区珊瑚沙路49号之江银泰百货一楼1031 T-WORLD"
			},
			{
                "title": "T-WORLD品丰温州吾悦广场店",
				"address": "浙江省温州市龙湾区雁荡西路与上江路交叉口温州吾悦广场一楼1045 T-WORLD"
			}
		]';

        return json_decode($PF, true);
    }
    private function getPC() :array{
        $PC = '[{
            "title": "华为授权体验店PLUS（湖滨银泰in77店）",
			"address": "杭州市上城区延安路185号湖滨银泰in77E区华为授权体验店plus"
		},
		{
            "title": "华为专卖店（武林银泰店）",
		    "address": "杭州市下城区延安路530号，武林银泰B馆二楼华为店"
		},
		{
            "title": "华为专卖店（杭州嘉里中心店）",
		    "address": "杭州市下城区延安路385号 3幢 负一楼华为"
		},
		{
            "title": "华为专卖店（杭州西湖银泰店）",
		    "address": "杭州市上城区延安南路98号西湖银泰一楼B馆华为店"
		},
		{
            "title": "华为授权体验店（下城新天地店）",
			"address": "杭州市下城区长浜路718号新天地购物中心一楼华为授权体验店"
		},
		{
            "title": "华为专卖店（杭州庆春银泰店）",
		    "address": "浙江省杭州市江干区景昙路18-26号一楼华为"
		},
		{
            "title": "华为专卖店（乐堤港店）",
			"address": "浙江省杭州市拱墅区远洋乐堤港一层华为店"
		},
		{
            "title": "华为授权体验店PLUS（大悦城店）",
			"address": "浙江省杭州市拱墅区隐秀路1号大悦城购物中心一楼华为授权体验店"
		},
		{
            "title": "华为授权体验店（西溪天街店）",
			"address": "杭州市西湖区龙湖西溪天街1楼51号华为授权体验店"
		},
		{
            "title": "华为授权体验店（理想银泰城店）",
			"address": "浙江省杭州市余杭区南苑街道世纪大道西一号临平银泰城华为专卖店F129"
		},
		{
            "title": "华为授权体验店（西溪印象城店）",
			"address": "浙江省杭州市余杭区五常大道1号西溪印象城印象城一楼68号P10168A华为授权店"
		},
		{
            "title": "华为授权体验店（金沙印象城店）",
			"address": "杭州市江干区金沙大道97号印象城华为体验店一楼1号门"
		},
		{
            "title": "华为专卖店（舜水南路店）",
			"address": "余姚市舜水南路141号"
		},
		{
            "title": "华为专卖店（阳明西路店）",
			"address": "余姚市阳明西路43号"
		},
		{
            "title": "华为授权体验店（五彩城店）",
			"address": "浙江省余姚市凤山街道城东路888号五彩城华为店"
		},
		{
            "title": "华为授权体验店（天一广场店）",
			"address": "宁波市海曙区日新街26号华为授权体验店天一广场店"
		},
		{
            "title": "华为专卖店（慈溪银泰店）",
			"address": "浙江省宁波市慈溪市浒山街道青少年宫路99号银泰一楼华为专柜"
		},
		{
            "title": "华为授权体验店（吾悦广场店）",
			"address": "浙江省慈溪市新城大道南路1449号慈溪新城吾悦广场1050"
		},
		{
            "title": "华为授权体验店（爱琴海购物公园店）",
			"address": "浙江省慈溪市新城大道北路与北三环东路交汇处（爱情海购物公园）F1008号华为店"
		},
		{
            "title": "华为专卖店（慈溪上林坊店）",
			"address": "浙江省慈溪市南门大街183号"
		},
		{
            "title": "华为授权体验店（大西洋银泰城店）",
		    "address": "浙江省温州市瓯海大道1299号银泰瓯海店1楼L1070"
		},
		{
            "title": "华为授权体验店（瑞安吾悦广场店）",
			"address": "浙江省瑞安市瑞祥大道1158号新城吾悦广场1F1019号 华为店"
		},
		{
            "title": "华为授权体验店（路之遥奥特莱斯广场店）",
			"address": "浙江省温州市乐清市北白象镇北环南路1号路之遥奥特莱斯广场16号楼114号商铺"
		},
		{
            "title": "华为专卖店（温州美美广场店）",
			"address": "浙江省温州市鹿城区鹿城路42号美美时尚广场华为专卖店"
		},
		{
            "title": "华为专卖店（海宁银泰店）",
		    "address": "浙江省海宁市海昌南路362号银泰城1楼126号华为店"
		},
		{
            "title": "华为专卖店（嘉兴平湖大润发店）",
			"address": "平湖大润发世纪商业中心2幢101—105"
		},
		{
            "title": "华为授权体验店（嘉善银泰店）",
			"address": "嘉善县罗星街道399号银泰百货1楼华为店"
		},
		{
            "title": "华为专卖店（绍兴柯桥银泰店）",
			"address": "浙江省绍兴市柯桥区笛扬路1531号柯桥银泰一楼华为店"
		},
		{
            "title": "华为专卖店（绍兴胜利东路店）",
		    "address": "浙江省绍兴市越城区胜利东路86号"
		},
		{
            "title": "华为专卖店（绍兴银泰店）",
			"address": "浙江省绍兴市越城区解放南路777号金帝银泰1F092号"
		},
		{
            "title": "华为授权体验店（雄风新天地店）",
			"address": "诸暨市浣东街道苎萝东路555号万风新天地东楼2号门"
		},
		{
            "title": "华为专卖店（台州椒江店）",
			"address": "台州市椒江区中山西路98号"
		},
		{
            "title": "华为专卖店（义乌吾悦广场店）",
			"address": "浙江省金华市义乌吾悦广场华为店1017"
		},
		{
            "title": "华为授权体验店（银泰城店）",
		    "address": "浙江省东阳市白云街道银泰城一楼华为体验店"
		},
		{
            "title": "华为专卖店（湖州红旗店）",
		    "address": "湖州市红旗路1号湖州大厦1楼话机世界华为店"
		},
		{
            "title": "华为授权体验店（仁皇山店）",
			"address": "浙江省湖州市吴兴区仁皇山生活广场华为授权体验店B座1楼11号"
		}
		]';

        return json_decode($PC, true);
    }

}
